package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;
import static com.example.tests.ContactInfoGenerator.generateRandomContacts;

public class TestBase {
	
	protected static ApplicationManager app;

	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(properties);
	  }
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	  }
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator() {
		List<Object[]> list = wrapGroupsForDataProvider(generateRandomGroups(2));
		return list.iterator();
	}
	
	public static List<Object[]> wrapGroupsForDataProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[]{group});
		}
		return list;
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator() {
		List<Object[]> list = wrapContactsForDataProvider(generateRandomContacts(2));
		return list.iterator();
	}
	
	public static List<Object[]> wrapContactsForDataProvider(List<ContactInfo> contacts) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactInfo contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list;
	}

	/* @DataProvider
	public Iterator<Object[]> randomValidContactGenerator() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 10; i++) {
			ContactInfo contact = new ContactInfo()
			.withFirstName("first" + generateRandomString())
			.withLastName("last" + generateRandomString())
			.withEmail("mail" + generateRandomString())
			.withHomeNumber("home" + generateRandomString())
			.withMobileNumber("")
			.withWorkNumber("")
			.withEmail2("")
			.withAddress("updated" + (i+1));
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}*/
	


	public String generateRandomString() {
		Random rnd = new Random();
		if (rnd.nextInt(3) == 0) {
			return "";
		} else {
			return String.valueOf(rnd.nextInt());
			//return "test" + rnd.nextInt();
		}
	}
	
}
